<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="材质名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入材质名称" />
      </el-form-item>
      <el-form-item label="材质类型" prop="materialType">
        <el-select v-model="formData.materialType" placeholder="请选择材质类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="材质类型id" prop="materialTypeId">
        <el-input v-model="formData.materialTypeId" placeholder="请输入材质类型id" />
      </el-form-item>
      <el-form-item label="楞别" prop="fluteType">
        <el-select v-model="formData.fluteType" placeholder="请选择楞别">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="层数" prop="layer">
        <el-input v-model="formData.layer" placeholder="请输入层数" />
      </el-form-item>
      <el-form-item label="密度" prop="density">
        <el-input v-model="formData.density" placeholder="请输入密度" />
      </el-form-item>
      <el-form-item label="厚度" prop="thickness">
        <el-input v-model="formData.thickness" placeholder="请输入厚度" />
      </el-form-item>
      <el-form-item label="耐破" prop="burstStrength">
        <el-input v-model="formData.burstStrength" placeholder="请输入耐破" />
      </el-form-item>
      <el-form-item label="边压" prop="edgeCompression">
        <el-input v-model="formData.edgeCompression" placeholder="请输入边压" />
      </el-form-item>
      <el-form-item label="面纸" prop="facePaper">
        <el-input v-model="formData.facePaper" placeholder="请输入面纸" />
      </el-form-item>
      <el-form-item label="瓦楞纸1" prop="flutePaper1">
        <el-input v-model="formData.flutePaper1" placeholder="请输入瓦楞纸1" />
      </el-form-item>
      <el-form-item label="芯纸1" prop="corePaper1">
        <el-input v-model="formData.corePaper1" placeholder="请输入芯纸1" />
      </el-form-item>
      <el-form-item label="瓦楞纸2" prop="flutePaper2">
        <el-input v-model="formData.flutePaper2" placeholder="请输入瓦楞纸2" />
      </el-form-item>
      <el-form-item label="芯纸2" prop="corePaper2">
        <el-input v-model="formData.corePaper2" placeholder="请输入芯纸2" />
      </el-form-item>
      <el-form-item label="瓦楞纸3" prop="flutePaper3">
        <el-input v-model="formData.flutePaper3" placeholder="请输入瓦楞纸3" />
      </el-form-item>
      <el-form-item label="里纸" prop="innerPaper">
        <el-input v-model="formData.innerPaper" placeholder="请输入里纸" />
      </el-form-item>
      <el-form-item label="供应商备注" prop="supplierRemark">
        <el-input v-model="formData.supplierRemark" placeholder="请输入供应商备注" />
      </el-form-item>
      <el-form-item label="成本单价" prop="costPrice">
        <el-input v-model="formData.costPrice" placeholder="请输入成本单价" />
      </el-form-item>
      <el-form-item label="密度单位" prop="densityUnit">
        <el-input v-model="formData.densityUnit" placeholder="请输入密度单位" />
      </el-form-item>
      <el-form-item label="是否库存预警" prop="stockWarning">
        <el-radio-group v-model="formData.stockWarning">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="库存下限" prop="stockLowerLimit">
        <el-input v-model="formData.stockLowerLimit" placeholder="请输入库存下限" />
      </el-form-item>
      <el-form-item label="库存上限" prop="stockUpperLimit">
        <el-input v-model="formData.stockUpperLimit" placeholder="请输入库存上限" />
      </el-form-item>
      <el-form-item label="面纸名称" prop="facePaperName">
        <el-input v-model="formData.facePaperName" placeholder="请输入面纸名称" />
      </el-form-item>
      <el-form-item label="瓦楞纸1名称" prop="flutePaper1Name">
        <el-input v-model="formData.flutePaper1Name" placeholder="请输入瓦楞纸1名称" />
      </el-form-item>
      <el-form-item label="瓦楞纸2名称" prop="flutePaper2Name">
        <el-input v-model="formData.flutePaper2Name" placeholder="请输入瓦楞纸2名称" />
      </el-form-item>
      <el-form-item label="瓦楞纸3名称" prop="flutePaper3Name">
        <el-input v-model="formData.flutePaper3Name" placeholder="请输入瓦楞纸3名称" />
      </el-form-item>
      <el-form-item label="里纸名称" prop="innerPaperName">
        <el-input v-model="formData.innerPaperName" placeholder="请输入里纸名称" />
      </el-form-item>
      <el-form-item label="瓦楞纸1坑型" prop="flutePaper1FluteType">
        <el-select v-model="formData.flutePaper1FluteType" placeholder="请选择瓦楞纸1坑型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="瓦楞纸2坑型" prop="flutePaper2FluteType">
        <el-select v-model="formData.flutePaper2FluteType" placeholder="请选择瓦楞纸2坑型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="瓦楞纸3坑型" prop="flutePaper3FluteType">
        <el-select v-model="formData.flutePaper3FluteType" placeholder="请选择瓦楞纸3坑型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="生管对应名称" prop="productionCorrespondingName">
        <el-input v-model="formData.productionCorrespondingName" placeholder="请输入生管对应名称" />
      </el-form-item>
      <el-form-item label="配置" prop="configuration">
        <el-input v-model="formData.configuration" placeholder="请输入配置" />
      </el-form-item>
      <el-form-item label="是否为基础材质" prop="isBaseMaterial">
        <el-radio-group v-model="formData.isBaseMaterial">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="科目" prop="subject">
        <el-input v-model="formData.subject" placeholder="请输入科目" />
      </el-form-item>
      <el-form-item label="是否停用" prop="isDisabled">
        <el-radio-group v-model="formData.isDisabled">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生管材质对照2" prop="productionMaterialCorrespondence2">
        <el-input v-model="formData.productionMaterialCorrespondence2" placeholder="请输入生管材质对照2" />
      </el-form-item>
      <el-form-item label="生管材质对照3" prop="productionMaterialCorrespondence3">
        <el-input v-model="formData.productionMaterialCorrespondence3" placeholder="请输入生管材质对照3" />
      </el-form-item>
      <el-form-item label="生管材质对照4" prop="productionMaterialCorrespondence4">
        <el-input v-model="formData.productionMaterialCorrespondence4" placeholder="请输入生管材质对照4" />
      </el-form-item>
      <el-form-item label="生管材质对照5" prop="productionMaterialCorrespondence5">
        <el-input v-model="formData.productionMaterialCorrespondence5" placeholder="请输入生管材质对照5" />
      </el-form-item>
      <el-form-item label="生管材质对照6" prop="productionMaterialCorrespondence6">
        <el-input v-model="formData.productionMaterialCorrespondence6" placeholder="请输入生管材质对照6" />
      </el-form-item>
      <el-form-item label="生管材质对照7" prop="productionMaterialCorrespondence7">
        <el-input v-model="formData.productionMaterialCorrespondence7" placeholder="请输入生管材质对照7" />
      </el-form-item>
      <el-form-item label="面纸克重" prop="facePaperWeight">
        <el-input v-model="formData.facePaperWeight" placeholder="请输入面纸克重" />
      </el-form-item>
      <el-form-item label="瓦楞纸1克重" prop="flutePaper1Weight">
        <el-input v-model="formData.flutePaper1Weight" placeholder="请输入瓦楞纸1克重" />
      </el-form-item>
      <el-form-item label="芯纸1克重" prop="corePaper1Weight">
        <el-input v-model="formData.corePaper1Weight" placeholder="请输入芯纸1克重" />
      </el-form-item>
      <el-form-item label="瓦楞纸2克重" prop="flutePaper2Weight">
        <el-input v-model="formData.flutePaper2Weight" placeholder="请输入瓦楞纸2克重" />
      </el-form-item>
      <el-form-item label="芯纸2克重" prop="corePaper2Weight">
        <el-input v-model="formData.corePaper2Weight" placeholder="请输入芯纸2克重" />
      </el-form-item>
      <el-form-item label="瓦楞纸3克重" prop="flutePaper3Weight">
        <el-input v-model="formData.flutePaper3Weight" placeholder="请输入瓦楞纸3克重" />
      </el-form-item>
      <el-form-item label="里纸克重" prop="innerPaperWeight">
        <el-input v-model="formData.innerPaperWeight" placeholder="请输入里纸克重" />
      </el-form-item>
      <el-form-item label="默认库存单位" prop="defaultStorageUnit">
        <el-input v-model="formData.defaultStorageUnit" placeholder="请输入默认库存单位" />
      </el-form-item>
      <el-form-item label="代用材质" prop="substituteMaterial">
        <el-input v-model="formData.substituteMaterial" placeholder="请输入代用材质" />
      </el-form-item>
      <el-form-item label="物料id" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料id" />
      </el-form-item>
       <el-form-item label="基础价" prop="basePrice">
        <el-input v-model="formData.basePrice" placeholder="请输入基础价" />
      </el-form-item>
      <el-form-item label="旧价" prop="oldPrice">
        <el-input v-model="formData.oldPrice" placeholder="请输入旧价" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="起订数" prop="minimumOrderQuantity">
        <el-input v-model="formData.minimumOrderQuantity" placeholder="请输入起订数" />
      </el-form-item>
      <el-form-item label="数量优惠" prop="quantityDiscount">
        <el-radio-group v-model="formData.quantityDiscount">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="供应商材质名称" prop="supplierMaterialName">
        <el-input v-model="formData.supplierMaterialName" placeholder="请输入供应商材质名称" />
      </el-form-item>
      <el-form-item label="基础材质" prop="baseMaterial">
        <el-radio-group v-model="formData.baseMaterial">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="加价" prop="markup">
        <el-input v-model="formData.markup" placeholder="请输入加价" />
      </el-form-item>
      <el-form-item label="最小厚度" prop="minThickness">
        <el-input v-model="formData.minThickness" placeholder="请输入最小厚度" />
      </el-form-item>
      <el-form-item label="最大厚度" prop="maxThickness">
        <el-input v-model="formData.maxThickness" placeholder="请输入最大厚度" />
      </el-form-item>
      <el-form-item label="长" prop="length">
        <el-input v-model="formData.length" placeholder="请输入长" />
      </el-form-item>
      <el-form-item label="宽" prop="width">
        <el-input v-model="formData.width" placeholder="请输入宽" />
      </el-form-item>
      <el-form-item label="报价单位" prop="quotationUnit">
        <el-input v-model="formData.quotationUnit" placeholder="请输入报价单位" />
      </el-form-item>
      <el-form-item label="不按原纸报价计算单价" prop="notBasedOnBasePaperUnitPrice">
        <el-radio-group v-model="formData.notBasedOnBasePaperUnitPrice">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="启用" prop="enable">
        <el-input v-model="formData.enable" placeholder="请输入启用" />
      </el-form-item>
      <el-form-item label="特价" prop="specialPrice">
        <el-input v-model="formData.specialPrice" placeholder="请输入特价" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="最小克重" prop="minDensity">
        <el-input v-model="formData.minDensity" placeholder="请输入最小克重" />
      </el-form-item>
      <el-form-item label="最大克重" prop="maxDensity">
        <el-input v-model="formData.maxDensity" placeholder="请输入最大克重" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PurRawMaterialQuotationApi } from '@/api/erp/purrawmaterialquotation'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  materialName: undefined,
  materialType: undefined,
  materialTypeId: undefined,
  fluteType: undefined,
  layer: undefined,
  density: undefined,
  thickness: undefined,
  burstStrength: undefined,
  edgeCompression: undefined,
  facePaper: undefined,
  flutePaper1: undefined,
  corePaper1: undefined,
  flutePaper2: undefined,
  corePaper2: undefined,
  flutePaper3: undefined,
  innerPaper: undefined,
  supplierRemark: undefined,
  costPrice: undefined,
  densityUnit: undefined,
  stockWarning: undefined,
  stockLowerLimit: undefined,
  stockUpperLimit: undefined,
  facePaperName: undefined,
  flutePaper1Name: undefined,
  flutePaper2Name: undefined,
  flutePaper3Name: undefined,
  innerPaperName: undefined,
  flutePaper1FluteType: undefined,
  flutePaper2FluteType: undefined,
  flutePaper3FluteType: undefined,
  productionCorrespondingName: undefined,
  configuration: undefined,
  isBaseMaterial: undefined,
  subject: undefined,
  isDisabled: undefined,
  productionMaterialCorrespondence2: undefined,
  productionMaterialCorrespondence3: undefined,
  productionMaterialCorrespondence4: undefined,
  productionMaterialCorrespondence5: undefined,
  productionMaterialCorrespondence6: undefined,
  productionMaterialCorrespondence7: undefined,
  facePaperWeight: undefined,
  flutePaper1Weight: undefined,
  corePaper1Weight: undefined,
  flutePaper2Weight: undefined,
  corePaper2Weight: undefined,
  flutePaper3Weight: undefined,
  innerPaperWeight: undefined,
  defaultStorageUnit: undefined,
  substituteMaterial: undefined,
  materialId: undefined,
  quotationId: undefined,
  basePrice: undefined,
  oldPrice: undefined,
  unitPrice: undefined,
  minimumOrderQuantity: undefined,
  quantityDiscount: undefined,
  supplierMaterialName: undefined,
  baseMaterial: undefined,
  markup: undefined,
  minThickness: undefined,
  maxThickness: undefined,
  length: undefined,
  width: undefined,
  quotationUnit: undefined,
  notBasedOnBasePaperUnitPrice: undefined,
  enable: undefined,
  specialPrice: undefined,
  remark: undefined,
  minDensity: undefined,
  maxDensity: undefined
})
const formRules = reactive({
  materialName: [{ required: true, message: '材质名称不能为空', trigger: 'blur' }],
  materialTypeId: [{ required: true, message: '材质类型id不能为空', trigger: 'blur' }],
  fluteType: [{ required: true, message: '楞别不能为空', trigger: 'change' }],
  layer: [{ required: true, message: '层数不能为空', trigger: 'blur' }],
  stockWarning: [{ required: true, message: '是否库存预警不能为空', trigger: 'blur' }],
  isBaseMaterial: [{ required: true, message: '是否为基础材质不能为空', trigger: 'blur' }],
  isDisabled: [{ required: true, message: '是否停用不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, quotationId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.quotationId = quotationId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PurRawMaterialQuotationApi.getPurRawMaterialQuotationDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await PurRawMaterialQuotationApi.createPurRawMaterialQuotationDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await PurRawMaterialQuotationApi.updatePurRawMaterialQuotationDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    materialName: undefined,
    materialType: undefined,
    materialTypeId: undefined,
    fluteType: undefined,
    layer: undefined,
    density: undefined,
    thickness: undefined,
    burstStrength: undefined,
    edgeCompression: undefined,
    facePaper: undefined,
    flutePaper1: undefined,
    corePaper1: undefined,
    flutePaper2: undefined,
    corePaper2: undefined,
    flutePaper3: undefined,
    innerPaper: undefined,
    supplierRemark: undefined,
    costPrice: undefined,
    densityUnit: undefined,
    stockWarning: undefined,
    stockLowerLimit: undefined,
    stockUpperLimit: undefined,
    facePaperName: undefined,
    flutePaper1Name: undefined,
    flutePaper2Name: undefined,
    flutePaper3Name: undefined,
    innerPaperName: undefined,
    flutePaper1FluteType: undefined,
    flutePaper2FluteType: undefined,
    flutePaper3FluteType: undefined,
    productionCorrespondingName: undefined,
    configuration: undefined,
    isBaseMaterial: undefined,
    subject: undefined,
    isDisabled: undefined,
    productionMaterialCorrespondence2: undefined,
    productionMaterialCorrespondence3: undefined,
    productionMaterialCorrespondence4: undefined,
    productionMaterialCorrespondence5: undefined,
    productionMaterialCorrespondence6: undefined,
    productionMaterialCorrespondence7: undefined,
    facePaperWeight: undefined,
    flutePaper1Weight: undefined,
    corePaper1Weight: undefined,
    flutePaper2Weight: undefined,
    corePaper2Weight: undefined,
    flutePaper3Weight: undefined,
    innerPaperWeight: undefined,
    defaultStorageUnit: undefined,
    substituteMaterial: undefined,
    materialId: undefined,
    quotationId: undefined,
    basePrice: undefined,
    oldPrice: undefined,
    unitPrice: undefined,
    minimumOrderQuantity: undefined,
    quantityDiscount: undefined,
    supplierMaterialName: undefined,
    baseMaterial: undefined,
    markup: undefined,
    minThickness: undefined,
    maxThickness: undefined,
    length: undefined,
    width: undefined,
    quotationUnit: undefined,
    notBasedOnBasePaperUnitPrice: undefined,
    enable: undefined,
    specialPrice: undefined,
    remark: undefined,
    minDensity: undefined,
    maxDensity: undefined
  }
  formRef.value?.resetFields()
}
</script>